package com.ys.controller.api.business;

import com.ys.config.MenuId;
import com.ys.entity.business.ThrOperateCompanyBrand;
import com.ys.entity.sys.YsCol;
import com.ys.service.business.ThrOperateCompanyBrandService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 运营公司表品牌信息
 */
@Controller
@RequestMapping("/api/inter/thrOperateCompanyBrand")
public class ApiInterThrOperateCompanyBrandController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterThrOperateCompanyBrandController.class);

    @Autowired
    private CommonApiService<ThrOperateCompanyBrand> commonApiService;

    @Autowired
    private ThrOperateCompanyBrandService thrOperateCompanyBrandService;

    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public ThrOperateCompanyBrand setSeachSql(ThrOperateCompanyBrand cond){
        cond.addLeftJoin("THR_OperateCompanyApply O ON O.Vgsbm = A.Vgsbm")
                .addField("O.Vgsjc AS vgsjc")
                .addLeftJoin("TS_UserChange U ON O.OperatorChangeId = U.Id")
                .addField("U.UserId AS userId")
        .addLeftJoin("TB_Country C ON C.Code = A.CountryCode")
                .addField("C.Name AS countryName")
        ;
        //排序
        cond.setOrderby("A.Vgsbm ASC");
        cond = commonApiService.dataAuth(MenuId.THR_OperateCompanyBrand, cond);
        return  cond;
    }

    /**
     * 查询
     */
    @RequiresPermissions("thrOperateCompanyBrand:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute ThrOperateCompanyBrand cond
    ){
        try {
            cond = setSeachSql(cond);
            return commonApiService.searchLay(cond);
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }

    /**
     * 导出
     */
    @RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute ThrOperateCompanyBrand cond){
        try {
            cond.setPage(0).setSize(999999);
            cond = setSeachSql(cond);
            //获取菜单字段数据
            List<YsCol> listField = new YsCol()
                    .setMenuId(MenuId.THR_OperateCompanyBrand)
                    .setFrontShow(1)
                    .setOrderby("A.SortNo asc")
                    .queryList();
            //获取数据
            List<Map<String, Object>> listData = cond.queryMap();

            //处理数据
            ExportExcelUtils<ThrOperateCompanyBrand> util = new ExportExcelUtils<>(new ThrOperateCompanyBrand());
            String toFilePath = util.exportExcel(listData, listField, "运营公司表品牌信息");
            UploadService.UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
            return R.ok(dto.getUrl());
        }catch (Exception e){
            logger.error("导出出错：",e.getMessage(),e);
            return R.error("导出失败，请稍后重试！");
        }
    }



    /**
     * 新增
     */
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toSave(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //新增
            return thrOperateCompanyBrandService.add(params,MenuId.THR_OperateCompanyBrand);
        } catch (Exception e) {
            logger.error("新增出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 修改
     */
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            //修改
            return thrOperateCompanyBrandService.update(params,MenuId.THR_OperateCompanyBrand);
        } catch (Exception e) {
            logger.error("修改出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 批量删除信息
     */
    @RequestMapping("/toDelByList")
    @ResponseBody
    public R toDelByList(HttpServletRequest request,@RequestParam Map<String, Object> params){
        try {
            String ids = params.get("ids").toString();
            String[] idArr = ids.split(",");
            List<String> idList = new ArrayList<>();
            Collections.addAll(idList,idArr);
            for(int i=0;i<idList.size();i++) {
                //删除
                thrOperateCompanyBrandService.del(Long.parseLong(idList.get(i)));
            }
            return R.ok();
        }catch (Exception e) {
            logger.error("删除出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }





}